#include <stdio.h>
#include <time.h>
#include <assert.h>
#include "image.h"
#include "data.h"
#include "network.h"

load_args get_base_args(network *net) {
    load_args args = { 0 };
    args.w = net->w;
    args.h = net->h;
    args.size = net->w;

    args.min = net->min_crop;
    args.max = net->max_crop;
    args.angle = net->angle;
    args.aspect = net->aspect;
    args.exposure = net->exposure;
    args.center = net->center;
    args.saturation = net->saturation;
    args.hue = net->hue;
    return args;
}

network* load_network(char *cfg, char *weights, int clear) {
    network *net = parse_network_cfg(cfg);
    if (weights && weights[0] != 0) {
        load_weights(net, weights);
    }
    if (clear) (*net->seen) = 0;
    return net;
}

float train_network_sgd(network *net, data d, int n) {
    int batch = net->batch;

    int i;
    float sum = 0;
    for (i = 0; i < n; ++i) {
        get_random_batch(d, batch, net->input, net->truth);
        float err = train_network_datum(net);
        sum += err;
    }
    return (float)sum / (n * batch);
}

network* make_network(int n) {
    network *net = calloc(1, sizeof(network));
    net->n = n;
    net->layers = calloc(net->n, sizeof(layer));
    net->seen = calloc(1, sizeof(size_t));
    net->t = calloc(1, sizeof(int));
    net->cost = calloc(1, sizeof(float));
    return net;
}